﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KP_CommonLibrary.KP_DataAccess;
using KP_CommonLibrary.KP_Business;
using KP_CommonLibrary.KP_CommonControls;
using KP_CommonLibrary;
using KP_CommonLibrary.Logs;
using KEEPER_BRR_13dotNET.BusinessLogic;

namespace KEEPER_BRR_13dotNET.Presentation.List
{
    public partial class Frm_DmNganhEdit : KP_CommonLibrary.KP_CoreUI.KP_EditListItem
    {
        public Frm_DmNganhEdit()
        {
            InitializeComponent();
        }
        public override void LoadDependencyData()
        {
            //load nhom nganh
            KP_BLListBase bNhomNganh = new KP_BLListBase("tblDmNhomNganh");
            cbNhomNganh.DataSource = bNhomNganh.Select();
            base.LoadDependencyData();
        }
        public override void CustomReadData(DataRow row)
        {
            try
            {
                string maLoaiHs = "000000";
                if (!row.IsNull("Ma_Nganh"))
                {
                    maLoaiHs = row["Ma_Nganh"].ToString();
                }
                bsGiayToKemTheo.DataSource = (Business as BL_KPDmNganh).SelectGiayToKemTheo(maLoaiHs);
                base.CustomReadData(row);
            }
            catch (Exception ex)
            {

                LogUtil.Logger.Fatal("Có lỗi khi lấy thành phần hồ sơ cho ngành nghề " + row["Ten_Nganh"].ToString(), ex);
                KPMessageBox.Show("Có lỗi khi lấy thành phần hồ sơ\n" + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }
        public override void SaveAndClose()
        {
            string state = "";
            try
            {

                state = "Kiểm tra dữ liệu\n";
                if (ValidateAll())
                {
                    state = "Kết thúc cập nhật\n";
                    EndEdit();
                    UpdateMaDoiTuong();
                    state = "Lưu dữ liệu\n";
                    if (FormState == KP_CommonLibrary.KP_CoreUI.FormState.Insert)
                    {
                        (Business as BL_KPDmNganh).Insert(CurrentRow, bsGiayToKemTheo.DataSource as DataTable);

                    }
                    else
                    {
                        (Business as BL_KPDmNganh).Update(CurrentRow, bsGiayToKemTheo.DataSource as DataTable);
                    }
                    CurrentRow.AcceptChanges();
                    FormState = KP_CommonLibrary.KP_CoreUI.FormState.Normal;
                    Close();
                }
            }
            catch (Exception ex)
            {
                KPMessageBox.Show(state + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);
                //Log vao log
            }
        }
        public override void SaveAndNew()
        {
            string state = "Lưu thông tin\n";
            try
            {
                state = "Kết thúc cập nhật\n";
                EndEdit();
                UpdateMaDoiTuong();
                state = "Kiểm tra dữ liệu\n";
                if (ValidateAll())
                {
                    state = "Lưu thông tin\n";
                    if (FormState == KP_CommonLibrary.KP_CoreUI.FormState.Insert)
                    {
                        (Business as BL_KPDmNganh).Insert(CurrentRow, bsGiayToKemTheo.DataSource as DataTable);

                    }
                    else
                    {
                        (Business as BL_KPDmNganh).Update(CurrentRow, bsGiayToKemTheo.DataSource as DataTable);
                    }
                    CurrentRow.AcceptChanges();
                    FormState = KP_CommonLibrary.KP_CoreUI.FormState.Unspecified;
                    state = "Khởi tạo bản ghi mới\n";
                    AddNew(ParentBinding);
                }
            }
            catch (Exception ex)
            {
                KPMessageBox.Show(state + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);
                //Log vao log
            }
        }
        private void UpdateMaDoiTuong()
        {
            DataTable tbl = bsGiayToKemTheo.DataSource as DataTable;
            if (tbl != null)
            {
                foreach (DataRow row in tbl.Rows)
                {
                    row["Ma_DoiTuong"] = txtMaNganh.Text;
                    row["Loai_DoiTuong"] = 2;
                }
            }
        }

        private void biAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtMaNganh.Text))
                {
                    KPMessageBox.Show("Hãy nhập mã ngành trước!", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.YesNo, KPMessageBoxIcon.Warning);
                    return;
                }
                Frm_GiayToKemTheoEdit frm = new Frm_GiayToKemTheoEdit();
                frm.LoaiDoiTuong = "2";
                frm.DoiTuongId = txtMaNganh.Text;
                CurrencyManager cm = (CurrencyManager)BindingContext[bsGiayToKemTheo];
                frm.AddNew(cm);
                DialogResult result = frm.ShowDialog();
                bsGiayToKemTheo.ResetBindings(true);

            }
            catch (Exception ex)
            {
                bsGiayToKemTheo.CancelEdit();
                KPMessageBox.Show("Thêm mới: " + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);
            }
        }

        private void biEdit_Click(object sender, EventArgs e)
        {
            try
            {

                if (bsGiayToKemTheo.Position >= 0 && bsGiayToKemTheo.DataSource != null)
                {
                    if (string.IsNullOrEmpty(txtMaNganh.Text))
                    {
                        KPMessageBox.Show("Hãy nhập mã ngành trước!", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.YesNo, KPMessageBoxIcon.Warning);
                        return;
                    }
                    Frm_GiayToKemTheoEdit frm = new Frm_GiayToKemTheoEdit();
                    frm.DoiTuongId = txtMaNganh.Text;
                    frm.LoaiDoiTuong = "2";
                    CurrencyManager cm = (CurrencyManager)BindingContext[bsGiayToKemTheo];
                    frm.Edit(cm);
                    DialogResult result = frm.ShowDialog();
                    bsGiayToKemTheo.ResetBindings(true);
                }
            }
            catch (Exception ex)
            {
                KPMessageBox.Show("Sửa : " + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);
            }
        }

        private void biDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DataRow current = grdTpHoSo.GetCurrentRow();
                if (current != null)
                {
                    DialogResult resul = KPMessageBox.Show("Bạn muốn xóa dòng đang chọn?", KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.YesNo, KPMessageBoxIcon.Question);
                    if (resul == DialogResult.Yes)
                    {
                        current.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                KPMessageBox.Show("Xóa : " + ex.Message, KP_SYS_VARIABLE.M_KEEPER_VER, KPMessageBoxButton.OK, KPMessageBoxIcon.Error);

            }
        }
    }
}
